package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.domain.EduMemberCourse;
import com.jf3q.common.utils.JwtTokenUtil;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.common.vo.SubjectVo;
import com.jf3q.front.service.ChapterService;
import com.jf3q.front.service.CourseDescriptionService;
import com.jf3q.front.service.MemberCourseService;
import com.jf3q.front.service.MemberService;
import com.jf3q.front.vo.CourseWebVo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class CourseController {

    @Autowired
    CourseDescriptionService courseDescriptionService;

    @Autowired
    MemberCourseService memberCourseService;

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @Autowired
    MemberService memberService;


    @GetMapping("/eduservice/coursefront/getFrontCourseInfo/{id}")
    public ResultVo getFrontCourseInfo(@PathVariable String id, HttpServletRequest request){
        //根据token解析出会员id
        String token = request.getHeader("token");
        String name = jwtTokenUtil.getUserNameFromToken(token);

        LambdaQueryWrapper<EduMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduMember::getMobile,name);
        EduMember member = memberService.getOne(wrapper);

        //会员id
        String memId=member.getId();
        log.info("会员id====="+memId);

        Map<String,Object> map = new HashMap<>();


        //课程信息
        CourseWebVo courseWebVo = courseDescriptionService.getCourseById(id);
        map.put("courseWebVo",courseWebVo);

        //判断会员是否购买了课程
        Boolean isBuy;

        LambdaQueryWrapper<EduMemberCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduMemberCourse::getCourseId,id);

        if (memId != null) {
            queryWrapper.eq(EduMemberCourse::getMemberId,memId);

            if (memberCourseService.getOne(queryWrapper) != null) {
                isBuy=true;
            }else {
                isBuy=false;
            }
        }else {
            isBuy=false;
        }


        map.put("isBuy",isBuy);


        return ResultVo.success(map);
    }

}
